package com.example.graduationdesignbackend.controller;


import cn.dev33.satoken.annotation.SaCheckLogin;
import cn.dev33.satoken.annotation.SaCheckRole;
import com.example.graduationdesignbackend.constants.ResponseCode;
import com.example.graduationdesignbackend.dto.MerchantDto;
import com.example.graduationdesignbackend.dto.PageDto;
import com.example.graduationdesignbackend.entity.Merchant;
import com.example.graduationdesignbackend.exception.ParkException;
import com.example.graduationdesignbackend.query.MerchantQuery;
import com.example.graduationdesignbackend.res.Res;
import com.example.graduationdesignbackend.service.IMerchantService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 园区周边 前端控制器
 * </p>
 *
 * @author yuze
 * @since 2024-04-21
 */
@RestController
@SaCheckLogin
@SaCheckRole("super-admin")
@RequestMapping("/merchant")
public class MerchantController {
    @Autowired
    private IMerchantService merchantService;

    @PostMapping
    public Res<String> createMerchant(@RequestBody MerchantDto merchantDto){
        return merchantService.createMerchant(merchantDto);
    }

    @GetMapping
    public Res<Merchant> getMerchantInfo(String id){
        return Res.yuze(merchantService.getById(id));
    }

    @DeleteMapping
    public Res<String> deleteMerchant(String id){
        Boolean res = merchantService.removeById(id);
        if (!res){
            throw new ParkException(ResponseCode.DeleteError);
        }
        return Res.yuze("删除成功");
    }

    @PutMapping
    public Res<String> editMerchant(@RequestBody MerchantDto merchantDto){
        return merchantService.editMerchant(merchantDto);
    }

    @GetMapping("/page")
    public Res<PageDto<Merchant>> getMerchantPages(MerchantQuery merchantQuery){
        return Res.yuze(merchantService.getMerchantPage(merchantQuery));
    }
}
